import { resolve } from "url"
import * as RouteApi from "@/api/route/route.js" 

export default {
    namespaced: true, 
	state:{
		requestRoute:{},
		id:0,
		name:"",
		options:{},
		category:"",
		preparedAttractions:[],
		routeAttractions:[],
		time:"",
		amount:0,
		distance:0,
		description:""
	},
	mutations:{
		SET_OPTIONS(state,value){
			state.options = value
		},
		SET_PRE_ATTRACTIONS(state,value){
			state.preparedAttractions = value
		},
		SET_ROUTE_ATTRACTIONS(state,value){
			state.routeAttractions = value
		},
		SET_ROUTE_DISTANCE(state,value){
			state.distance = value
		},
		SET_ROUTE_CATEGORY(state,value){
			state.category = value
		},
		SET_ROUTE_NAME(state,value){
			state.name = value
		},
		SET_ROUTE_TIME(state,value){
			state.time = value
		},
		SET_ROUTE_AMOUNT(state,value){
			state.amount = value
		},
		SET_ROUTE_ID(state,value){
			state.id = value
		},
		SET_REQUEST_ROUTE(state,value){
			state.requestRoute = value
		}
		
	},
	actions:{
		setOptions(context,value){
			context.commit('SET_OPTIONS',value)
		},
		setPreAttractions(context,value){
			context.commit('SET_PRE_ATTRACTIONS',value)
		},
		setRouteAttractions(context,value){
			context.commit('SET_ROUTE_ATTRACTIONS',value)
		},
		setRouteDistance(context,value){
			context.commit('SET_ROUTE_DISTANCE',value)
		},
		setRouteName(context,value){
			context.commit('SET_ROUTE_NAME',value)
		},
		setRouteTime(context,value){
			context.commit('SET_ROUTE_TIME',value)
		},
		setRouteAmount(context,value){
			context.commit('SET_ROUTE_AMOUNT',value)
		},
		setRouteCategory(context,value){
			context.commit('SET_ROUTE_CATEGORY',value)
		},
		setRouteId(context,value){
			context.commit('SET_ROUTE_ID',value)
		},
		clearRouteInfo({commit}){
			commit("SET_OPTIONS",{})
			commit("SET_PRE_ATTRACTIONS",[])
			commit("SET_ROUTE_ATTRACTIONS",[])
			commit("SET_ROUTE_DISTANCE",0)
			commit("SET_ROUTE_CATEGORY","")
			commit("SET_ROUTE_NAME","")
			commit("SET_ROUTE_TIME",0)
			commit("SET_ROUTE_AMOUNT",0)
		},
		setRoute({commit},value){
			commit("SET_ROUTE_ID",value.id)
			commit("SET_PRE_ATTRACTIONS",value.routeAttractionsList)
			commit("SET_ROUTE_DISTANCE",value.distance)
			commit("SET_ROUTE_CATEGORY",value.category)
			commit("SET_ROUTE_NAME",value.name)
			commit("SET_ROUTE_TIME",value.time)
			commit("SET_ROUTE_AMOUNT",value.amount)	
		},
		submitRouteInfo({getters}){
			const route =getters.route
			return new Promise((resolve,reject) => {
				RouteApi.makeRoute(route).then(res => resolve(res))
				  .catch(reject)
			})
		},
		updateRouteInfo({getters}){
			const route = getters.route
			return new Promise((resolve,reject) => {
				RouteApi.updateRoute(route).then(res => resolve(res))
				  .catch(reject)
			})
		}
		,
		setRequestRoute(context,value){
			context.commit('SET_REQUEST_ROUTE',value)
		}
	
	},
	getters:{
		routeOptions(state){
			return state.options
		},
		routeAttractions(state){
			return state.attractions
		},
		routeDistance(state){
			return state.distance
		},
		route(state){
			return {
				routeId:state.id,
				name:state.name,
				category:state.category,
				routeAttractionsList:state.routeAttractions,
				time:state.time,
				amount:state.amount,
				distance:state.distance,
				description:state.description
			}
		}
	}
}